package com.watson.bloom;

import com.google.common.hash.BloomFilter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @program: mytest
 * @description: guava test
 * @author: zhangpeng348@jd.com
 * @date: 2018-12-10 20:40
 **/
public class GuavaTest {
    private static Logger logger = LoggerFactory.getLogger(GuavaTest.class);

    private BloomFilter<String> bloomFilter;

    public GuavaTest() {
        MyFunnel myFunnel = new MyFunnel();
        bloomFilter = BloomFilter.create(myFunnel,
                1024*1024*100,
                0.0001d);
    }

    public synchronized boolean containElement(String ele) {
        if (StringUtils.isEmpty(ele)) {
            logger.warn("ele is empty!");
            return false;
        }

        boolean exists = bloomFilter.mightContain(ele);
        if (!exists) {
            bloomFilter.put(ele);
        }
        return exists;
    }


    public static void main(String[] args) {
        GuavaTest guavaTest = new GuavaTest();
        System.out.println(guavaTest.containElement("1000000"));
        System.out.println(guavaTest.containElement("1000001"));
        System.out.println(guavaTest.containElement("1000002"));
        System.out.println(guavaTest.containElement("1000003"));
        System.out.println(guavaTest.containElement("1000004"));
        System.out.println(guavaTest.containElement("1000005"));
        System.out.println(guavaTest.containElement("1000000"));



    }
}
